﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using AmazeUI.bll;
using AmazeUI.bll.GetModel;
using AmazeUI.bll.Open;
using AmazeUI.bll.Open.Weixin;
using AmazeUI.bll.Open.WeixinPay;
using AmazeUI.db;
using Dbx.web.Controllers;
using Dbx.web.Models.XtModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WebCommon;
using WebCommon.StringCommand;

namespace Am.Test
{
   public class xml
    {
       public string S { get; set; }

       public string S1 { get; set; }

       public xml()
       {
           this.S = "asdsfff";
           this.S1 = "qqqqqqqq";
       }
    }
   public class Foo
   {
       public string fn { get; set; }
       public string tradeid { get; set; }
       public string price { get; set; }
       public string subject { get; set; }
       public string noticeurl { get; set; }
       public string returl { get; set; }
       public string key { get; set; }


   }
    [TestClass]
    public class UnitTest1
    {
        XtEntities db = new XtEntities();
        public string Serialize<T>(T t)
        {
            using (StringWriter sw = new StringWriter())
            {
                XmlSerializer xz = new XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                return sw.ToString();
            } 

        }

       
        [TestMethod]
        public void Sign2()
        {

        }
           
        [TestMethod]
        public void Sign()
        {
            //{"addrSign":"132bc484fe7ced9d592d0aae577b765ab3348325","accessToken":"grfNzndC9OpXRJxvLmctsmIdjCMKLUThecrE_5evt5YXwnr20KimDMYTieSWm8eReiH8g22UDz1pP0fGZHFBY8T-R8y2iLIQtGt3dmNZ3DA","Url":"http://hd.jjck.cn/wx/pay&code=02191dfc22fa8339361e208295337feR&state=http://hd.jjck.cn/wx/pay","signType":"sha1","scope":"jsapi_address","appId":"wx90b62494684aec82","timestamp":"1438679867","nonceStr":"AqL2o38Ima"
            var data = string.Format("accesstoken={0}&appid={1}&noncestr={2}&timestamp={3}&url={4}", "grfNzndC9OpXRJxvLmctsmIdjCMKLUThecrE_5evt5YXwnr20KimDMYTieSWm8eReiH8g22UDz1pP0fGZHFBY8T-R8y2iLIQtGt3dmNZ3DA", "wx90b62494684aec82", "AqL2o38Ima", "1438679867", "http://hd.jjck.cn/wx/pay&code=02191dfc22fa8339361e208295337feR&state=http://hd.jjck.cn/wx/pay");
            Debug.Write(WebCommon.Encryption.Sha1(data)); 
        }

        [TestMethod]
        public void TextopenUser()
        {
            var json =
                @"{""openid"":""okZ9pt1HjOFzCuhFfgKL9nKTMzBE"",""nickname"":""李帅"",""sex"":1,""language"":""zh_CN"",""city"":""南宁"",""province"":""广西"",""country"":""中国"",""headimgurl"":""http:\/\/wx.qlogo.cn\/mmopen\/SFbeS8Sw4TVRiakb6ENicWFEcEsl7CkVDXg7OhaZsBgNHbNvM2S0xwbqbmr41Rmj0HEVzb4ATym9XibNy1lFkTEc56EQrb4xjNe\/0"",""privilege"":[]}";
            XtUserBll XtUserBll=new XtUserBll();
            var j = JsonConvert.DeserializeObject<WxUserInfo>(json);
            var id = XtUserBll.AddXtUser(j);

              
            var xg =new XtUserBll().GetModel(id);
            OpenBll open = new OpenBll();
            var wu = open.SetWxUser(j, id);
          
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        [TestMethod]
        public void ChushihuaData()
        {
            //Create();

            //this.db.XtNav.Add(ZhuXtNav("系统用户", "am-icon-users", 1));
            //this.db.XtNav.Add(ZhuXtNav("系统设置", "am-icon-dashboard", 2));
            //this.db.XtNav.Add(ZhuXtNav("接口管理", "am-icon-level-up", 3));
            //this.db.XtNav.Add(FuXtNav("用户列表", "am-icon-database", 1, "user", "list"));
            //this.db.XtNav.Add(FuXtNav("角色列表", "am-icon-database", 1, "admin", "rolelist"));
            //this.db.XtNav.Add(FuXtNav("用户添加", "am-icon-user-plus", 1, "user", "add"));
            //this.db.XtNav.Add(FuXtNav("修改密码", "am-icon-lock", 2, "user", "pwd"));
            //this.db.XtNav.Add(FuXtNav("修改资料", "am-icon-info", 2, "user", "info"));
            //this.db.XtNav.Add(FuXtNav("菜单列表", "am-icon-user-plus", 2, "admin", "naslist"));
            //this.db.XtNav.Add(FuXtNav("接口测试", "am-icon-bug", 3, "api", "test"));
            //this.db.XtNav.Add(FuXtNav("接口列表", "am-icon-user-plus", 3, "api", "list"));

            //this.db.XtNav.Add(ZhuXtNav("文章管理", "", 4));
            //this.db.XtNav.Add(FuXtNav("添加文章", "", 4, "Article", "addArticle"));
            //this.db.XtNav.Add(FuXtNav("文章列表", "", 4, "Article", "list"));

            //this.db.XtNav.Add(ZhuXtNav("日志类", "", 5));
            //this.db.XtNav.Add(FuXtNav("系统日志", "", 5, "admin", "xtlog")); 
            // this.db.XtNav.Add(FuXtNav("异常日志", "", 5, "admin", "xterror"));

            this.db.XtNav.Add(ZhuXtNav("订单类", "", 6));
            this.db.XtNav.Add(FuXtNav("订单列表", "", 6, "admin", "payorder")); 
            this.db.SaveChanges();
        }
        /// <summary>
        /// 初始化选项
        /// </summary>
        void Create()
        {
            if (this.db.Database.Exists())
            {

                this.db.Database.Delete();
            }

            this.db.Database.Create();

            var xtPowers = new XtPowers();
            xtPowers.Name = "进入后台";
            this.db.XtPowers.Add(xtPowers);

            var xtPowers1 = new XtPowers();
            xtPowers1.Name = "普通";
            this.db.XtPowers.Add(xtPowers1);
            this.db.SaveChanges();

            var xtRole = new XtRole();
            xtRole.Name = "超级管理员";
            xtRole.Powers = xtPowers.Id.ToString();

            var xtRole1 = new XtRole();
            xtRole1.Name = "普通";
            xtRole1.Powers = xtPowers1.Id.ToString();
            this.db.XtRole.Add(xtRole1);
            this.db.SaveChanges();

            XtUserBll bll = new XtUserBll();
            bll.AddXtUser(Addadmin("admin", "admin", xtRole.Id));
        }


        SubXtUser Addadmin(string name, string pwd, int rid)
        {
            var model = new SubXtUser();
            model.Pwd = pwd;
            model.UserName = name;
            model.Name = "管理员";
            model.RoleId = rid;
            return model;
        }

        /// <summary>
        /// 主菜单
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ioc"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        XtNav ZhuXtNav(string name, string ioc, int sid)
        {
            var xtNav1 = new XtNav();
            xtNav1.Name = name;
            xtNav1.Ioc = ioc;
            xtNav1.ParentId = 0;
            xtNav1.Sid = sid;
            return xtNav1;
        }

        /// <summary>
        /// 副菜单
        /// </summary>
        /// <param name="name"></param>
        /// <param name="ioc"></param>
        /// <param name="pid"></param>
        /// <param name="controllers"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        XtNav FuXtNav(string name, string ioc, int pid, string controllers, string action)
        {
            var xtNav1 = new XtNav();
            xtNav1.Name = name;
            xtNav1.Ioc = ioc;
            xtNav1.ParentId = pid;
            xtNav1.Controllers = controllers;
            xtNav1.Action = action;
            xtNav1.Sid = 0;
            return xtNav1;
        }
        /// <summary>
        /// 添加商品
        /// </summary>
        /// <returns></returns>
        int AddGoods()
        {
            var g = new GoodsBll().AddGoods(new SubGoods());
            return g;
        }

    }
}
